import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useThemeStore = defineStore('theme', () => {
    const currentTheme = ref('light')

    const toggleTheme = () => {
        currentTheme.value = currentTheme.value === 'light' ? 'dark' : 'light'
        document.documentElement.setAttribute('data-theme', currentTheme.value)
        localStorage.setItem('theme', currentTheme.value)
    }

    // 初始化主题
    const initTheme = () => {
        const savedTheme = localStorage.getItem('theme') || 'light'
        currentTheme.value = savedTheme
        document.documentElement.setAttribute('data-theme', savedTheme)
    }

    return {
        currentTheme,
        toggleTheme,
        initTheme
    }
})